function getWeekNumber(year) {
    var d = new Date(year, 0, 1);
    while (d.getDay() != 1) {
        d.setDate(d.getDate() + 1);
    }
    var to = new Date(year + 1, 0, 1);
    var i = 0;
    for (var from = d; from < to;) {
        from.setDate(from.getDate() + 6);
        if (from < to) {
            from.setDate(from.getDate() + 1);
        } else {
            to.setDate(to.getDate() - 1);
        }
        i++;
    }
    return i;
}
var currYear = new Date().getFullYear();

(function () {
    var vueData = {
        weekCount: getWeekNumber(currYear),
        loading: false,
        spuList: [
            {spuCode: 'm02', spuName: '理想M02'},
            {spuCode: 'm01', spuName: '理想M01'}
        ],
        yearList: [
            {year: currYear},
            {year: currYear + 1},
            {year: currYear + 2}
        ],
        //选择的spu
        currSpuCode: 'm01',
        currYear: currYear,
        // 产量
        produceAmount:[],
        //大定订单量
        orderAmount:[],
        // 周末库存
        weekedStockAmount:[],
        // 库存系数
        stockRate:[],
        // 交付量
        deliveriedAmount:[],
        year:currYear,
        supCode:"m01",
        tableData2:[]
    };

    var vue = new Vue({
        el: "#app",
        data: vueData,
        methods: {
            queryList(year,supCode){
               let queryLongPlanVo ={year:year,spuCode:supCode};
                $.ajax({
                    url:  '/allotLong/queryList',
                    data: JSON.stringify(queryLongPlanVo),
                    type: 'post',
                    dataType: 'json',
                    contentType: "application/json;charset=UTF-8",
                    success: function (response) {
                        if (response.code == 0 && response.success) {
                            let list = response.data;
                            for(let i = 0; i< list.length;i++){
                                if ( list[i].itemType === 10){
                                   vueData.produceAmount =  list[i].rows;
                                }else if(list[i].itemType === 20){
                                    vueData.orderAmount =  list[i].rows;
                                }else if(list[i].itemType === 30){
                                    vueData.weekedStockAmount =  list[i].rows;
                                }else if(list[i].itemType === 40){
                                    vueData.stockRate =  list[i].rows;
                                }else if(list[i].itemType === 50){
                                    vueData.deliveriedAmount =  list[i].rows;
                                }

                            }

                          console.log(response)
                        } else {
                            vue.$message({message: '查询失败！', type: 'warning'});
                        }
                    },
                    error: function (xhr) {
                        vue.$message.error("查询失败:" + xhr.status);
                    }
                });
            },
            queryAllotLongBySpuList(){
              console.log(vueData.currSpuCode)
            },
            queryAllotLongByYearList(){
                console.log(vueData.currYear)
            }
        },
        mounted(){
            this.queryList(vueData.year,vueData.supCode)
        }
    });
})();